#include "Point3D.h"
#include <math.h>

Vector3D::Vector3D(void)
    : _x(0.0f), _y(0.0f), _z(0.0f)
{
}

Vector3D::Vector3D(double x, double y, double z)
    : _x(x), _y(y), _z(z)
{
}

Vector3D::~Vector3D(void)
{
}

double Vector3D::getL2Distance() const
{
        return sqrtf(powf(getX(), 2) + powf(getY(), 2) + powf(getZ(), 2));
}

Vector3D Vector3D::normalize() const
{
        float length = getL2Distance();

        if ( length == 0.0f )
                return Vector3D();
        else
                return Vector3D(getX() / length, getY() / length, getZ() / length);
}

Vector3D Vector3D::operator+(const Vector3D & v) const
{
        return Vector3D(getX() + v.getX(),
                        getY() + v.getY(),
                        getZ() + v.getZ());
}

Vector3D Vector3D::operator-(const Vector3D & v) const
{
        return Vector3D(getX() - v.getX(),
                        getY() - v.getY(),
                        getZ() - v.getZ());
}

Vector3D Vector3D::operator*(double scalar) const
{
        return Vector3D(getX() * scalar,
                        getY() * scalar,
                        getZ() * scalar);
}

double Vector3D::dotProduct(const Vector3D & v) const
{
        return getX() * v.getX() + getY() * v.getY() + getZ() * v.getZ();
}

Vector3D Vector3D::crossProduct(const Vector3D & v) const
{
        return Vector3D(getY() * v.getZ() - getZ() * v.getY(),
                        getZ() * v.getX() - getX() * v.getZ(),
                        getX() * v.getY() - getY() * v.getX());
}

double Vector3D::DotProduct(const Vector3D & u, const Vector3D & v)
{
        return u.getX() * v.getX() + u.getY() * v.getY() + u.getZ() * v.getZ();
}

Vector3D Vector3D::CrossProduct(const Vector3D & u, const Vector3D & v)
{
        return Vector3D(u.getY() * v.getZ() - u.getZ() * v.getY(),
                        u.getZ() * v.getX() - u.getX() * v.getZ(),
                        u.getX() * v.getY() - u.getY() * v.getX());
}

Vector3D Vector3D::getLinearCurve(const Vector3D & p1, const Vector3D & p2, double t)
{
        float dx = p2.getX() - p1.getX();
        float dy = p2.getY() - p1.getY();
        float dz = p2.getZ() - p1.getZ();

        float tx = p1.getX() + (dx * t);
        float ty = p1.getY() + (dy * t);
        float tz = p1.getZ() + (dz * t);

        return Vector3D(tx, ty, tz);
}
